package com.pktechan.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.pktechan.api.base.BaseRedisServiceImpl;
import com.pktechan.api.entity.CustBase;
import com.pktechan.api.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by kaifabu on 2018/9/11.
 */
@Service("redisService")
public class RedisServiceImpl extends BaseRedisServiceImpl implements RedisService {


    private static final Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);

    /**
     * 将小程序用户登录态保存到redis中。
     *
     * @param userLoginState 根据openid生成的唯一字符串
     * @param respMap        {"openid": "OPENID","session_key": "SESSIONKEY",}或{"openid": "OPENID","session_key": "SESSIONKEY","unionid": "UNIONID"}
     */
    @Override
    public void setLoginState(String userLoginState, HashMap<String, String> respMap) {
        setMap(userLoginState, respMap);
    }

    /**
     * 将小程序用户登录态保存到redis中。
     *
     * @param userLoginState 根据openid生成的唯一字符串
     * @return {"openid": "OPENID","session_key": "SESSIONKEY",}或{"openid": "OPENID","session_key": "SESSIONKEY","unionid": "UNIONID"}
     */
    @Override
    public Map<String, String> getLoginState(String userLoginState) {
        return getMap(userLoginState);
    }

    /**
     * 删除小程序登录态
     *
     * @param miniProgramSessionId
     */
    @Override
    public void delLoginState(String miniProgramSessionId) {
        delMap(miniProgramSessionId);
    }

    /**
     * 根据MPID获取AccessToken
     *
     * @param mpId
     * @return
     */
    @Override
    public String getAccessTokenByMpId(String mpId) {
        String accessTokenKey = mpId + "_accessToken";
        logger.debug("根据mpId获取AccessToken：" + mpId);
        return get(accessTokenKey);
    }

    /**
     * 保存AccessToken至redis中
     *
     * @param mpId
     * @param accessToken
     */
    @Override
    public void setAccessTokenByMpId(String mpId, String accessToken) {
        String accessTokenKey = mpId + "_accessToken";
        logger.debug("将微信公众号accessToken保存到redis中，accessTokenKey：" + accessTokenKey + "，accessToken：" + accessToken);
        set(accessTokenKey, accessToken);
        //access_token的有效期通过返回的expires_in来传达，正常情况下为7200秒（2小时）,redis设置键的超时时间为7000秒
        setExpire(accessTokenKey, 7000);
    }

    /**
     * 根据MPID获取SdkTicket
     *
     * @param mpId
     * @return
     */
    @Override
    public String getSdkTicketByMpId(String mpId) {
        String sdkTicketKey = mpId + "_ticketKey";
        logger.debug("根据mpId获取sdkTicketKey：" + mpId);
        return get(sdkTicketKey);
    }

    /**
     * 保存SdkTicket至redis中
     *
     * @param mpId
     * @param sdkTicket
     */
    @Override
    public void setSdkTicketByMpId(String mpId, String sdkTicket) {
        String sdkTicketKey = mpId + "_ticketKey";
        logger.debug("将微信公众号SdkTicket保存到redis中，SdkTicketKey：" + sdkTicketKey + "，sdkTicket：" + sdkTicket);
        set(sdkTicketKey, sdkTicket);
        //access_token的有效期通过返回的expires_in来传达，正常情况下为7200秒（2小时）,redis设置键的超时时间为7000秒
        setExpire(sdkTicketKey, 7000);
    }

    /**
     * 客户基本
     *
     * @param openId
     * @param custBaseJson
     */
    @Override
    public void setCustBaseByOpenId(String openId, String custBaseJson) {
        logger.debug("将用户信息保存到redis中，openId：" + openId + "，custBaseJson：" + custBaseJson);
        set(openId, custBaseJson);
        setExpire(openId, 7200);
    }

    /**
     * 根据openId获取客户基本信息
     *
     * @param openId
     */
    @Override
    public String getCustBaseByOpenId(String openId) {
        logger.debug("根据openId获取用户基本信息：" + openId + "----custBaseJson---" + get(openId));
        return get(openId);
    }

    /**
     * 根据openId清空redis
     *
     * @param openId
     */
    @Override
    public void deleteCustBaseByOpenId(String openId) {
        logger.info("根据openId清空redis：" + openId);
        long result =del(openId);
        logger.info("0表示指定的键不存在， 大于0表示删除一个或多个。：" + result);
    }

    /**
     * 将短信验证码保存到redis中有效期5分钟
     *
     * @param mobile
     * @param code
     */
    @Override
    public void setMsgCode(String mobile, String code) {
        logger.debug("将短信验证码保存到redis中有效期5分钟，mobile：" + mobile + "，code：" + code);
        set(mobile, code);
        setExpire(mobile, 300);
    }

    /**
     * 根据手机号查询验证码
     *
     * @param mobile
     * @return
     */
    @Override
    public String getMsgCode(String mobile) {
        logger.debug("根据手机号查询验证码：mobile：" + mobile + "，code：" + get(mobile));
        return get(mobile);
    }

    /**
     * 根据手机号删除验证码
     *
     * @param mobile
     */
    @Override
    public void delMsgCode(String mobile) {
        logger.debug("根据手机号删除验证码,mobile：" + mobile);
        del(mobile);
    }

    /**
     * 保存图形验证码
     *
     * @param userLoginState
     * @param imgCode
     */
    @Override
    public void setImageAuthCode(String userLoginState, String imgCode) {
        String imageAuthCodeKey = userLoginState + "_imageAuthCode";
        logger.debug("将图形验证码保存到redis中，userLoginState：" + userLoginState + "，imgCode：" + imgCode);
        set(imageAuthCodeKey, imgCode);
        setExpire(imageAuthCodeKey, 300);
    }

    /**
     * 获取图形验证码
     *
     * @param userLoginState
     * @return
     */
    @Override
    public String getImageAuthCode(String userLoginState) {
        String imageAuthCodeKey = userLoginState + "_imageAuthCode";
        logger.debug("根据客户ID查询验证码：userLoginState：" + userLoginState + "，code：" + get(imageAuthCodeKey));
        return get(imageAuthCodeKey);
    }

    /**
     * 根据客户登录态查询客户基本信息
     *
     * @param userLoginState
     */
    @Override
    public CustBase getCustBaseByUserLoginState(String userLoginState) {
        //获取用户登录信息
        logger.debug("根据客户登录态查询客户基本信息,getCustBaseByUserLoginState：userLoginState：" + userLoginState);
        HashMap<String, String> loginStateMap = (HashMap<String, String>) getLoginState(userLoginState);
        CustBase custBase = JSONObject.parseObject(getCustBaseByOpenId(loginStateMap.get("openid")), CustBase.class);
        logger.debug("客户信息：" + getCustBaseByOpenId(loginStateMap.get("openid")));
        return custBase;
    }

    /**
     * 将实名认证图形验证码保存到redis中有效期5分钟
     *
     * @param userLoginState
     * @param imgCode
     */
    @Override
    public void setRealNameImageAuthCode(String userLoginState, String imgCode) {
        String realNameImageAuthCodeKey = userLoginState + "_realNameImageAuthCode";
        logger.debug("将实名认证图形验证码保存到redis中，userLoginState：" + userLoginState + "，realNameImageAuthCode：" + imgCode);
        set(realNameImageAuthCodeKey, imgCode);
        setExpire(realNameImageAuthCodeKey, 300);
    }

    /**
     * 获取实名认证图形验证码
     *
     * @param userLoginState
     * @return
     */
    @Override
    public String getRealNameImageAuthCode(String userLoginState) {
        String realNameImageAuthCodeKey = userLoginState + "_realNameImageAuthCode";
        logger.debug("根据客户ID查询实名认证图片验证码：userLoginState：" + userLoginState + "，code：" + get(realNameImageAuthCodeKey));
        return get(realNameImageAuthCodeKey);
    }
}
